#include "StdAfx.h"
#include "EditorOptionsForm.h"

using namespace OgreUnified;

EditorOptionsForm::EditorOptionsForm(void)
{
	InitializeComponent();

	mRsList = const_cast<RenderSystemList*>(&Ogre::Root::getSingletonPtr()->getAvailableRenderers());
	RenderSystemList::iterator ie = mRsList->begin();
	
	for(; ie != mRsList->end(); ie++)
	{
		mComboBox->Items->Add(gcnew System::String((*ie)->getName().c_str()));
	}

	if(Ogre::Root::getSingletonPtr()->getRenderSystem())
	{
		for(int i=0;i<(int)mRsList->size();i++)
			if(Ogre::Root::getSingletonPtr()->getRenderSystem()->getName() == mRsList->at(i)->getName())
			{
				mComboBox->SelectedIndex=i;
				updateDatagrid();
			}
	}
	else
	{
		if(mComboBox->Items->Count>0)
		{
			mComboBox->SelectedIndex=0;
			updateDatagrid();
		}
	}
	mButtonSave->DialogResult = System::Windows::Forms::DialogResult::OK;
}

EditorOptionsForm::~EditorOptionsForm()
{
	if (components)
	{
		delete components;
	}
}

void EditorOptionsForm::InitializeComponent(void)
{
	this->mTabControl = (gcnew System::Windows::Forms::TabControl());
	this->mTabPageRenderSystem = (gcnew System::Windows::Forms::TabPage());
	this->mComboBox = (gcnew System::Windows::Forms::ComboBox());
	this->mLabel = (gcnew System::Windows::Forms::Label());
	this->mDataGridView = (gcnew System::Windows::Forms::DataGridView());
	this->mOptionName = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
	this->mOptionValue = (gcnew System::Windows::Forms::DataGridViewComboBoxColumn());
	this->mButtonSave = (gcnew System::Windows::Forms::Button());
	this->mButtonCancel = (gcnew System::Windows::Forms::Button());
	this->mTabControl->SuspendLayout();
	this->mTabPageRenderSystem->SuspendLayout();
	(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->mDataGridView))->BeginInit();
	this->SuspendLayout();
	// 
	// mTabControl
	// 
	this->mTabControl->Controls->Add(this->mTabPageRenderSystem);
	this->mTabControl->Dock = System::Windows::Forms::DockStyle::Top;
	this->mTabControl->Location = System::Drawing::Point(0, 0);
	this->mTabControl->Name = L"mTabControl";
	this->mTabControl->SelectedIndex = 0;
	this->mTabControl->Size = System::Drawing::Size(624, 402);
	this->mTabControl->TabIndex = 0;
	// 
	// mTabPageRenderSystem
	// 
	this->mTabPageRenderSystem->Controls->Add(this->mComboBox);
	this->mTabPageRenderSystem->Controls->Add(this->mLabel);
	this->mTabPageRenderSystem->Controls->Add(this->mDataGridView);
	this->mTabPageRenderSystem->Location = System::Drawing::Point(4, 22);
	this->mTabPageRenderSystem->Name = L"mTabPageRenderSystem";
	this->mTabPageRenderSystem->Padding = System::Windows::Forms::Padding(3);
	this->mTabPageRenderSystem->Size = System::Drawing::Size(616, 376);
	this->mTabPageRenderSystem->TabIndex = 0;
	this->mTabPageRenderSystem->Text = L"Ogre Renderer";
	this->mTabPageRenderSystem->UseVisualStyleBackColor = true;
	// 
	// mComboBox
	// 
	this->mComboBox->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
	this->mComboBox->FormattingEnabled = true;
	this->mComboBox->Location = System::Drawing::Point(93, 11);
	this->mComboBox->Name = L"mComboBox";
	this->mComboBox->Size = System::Drawing::Size(515, 21);
	this->mComboBox->TabIndex = 2;
	this->mComboBox->SelectionChangeCommitted += gcnew System::EventHandler(this, &EditorOptionsForm::mComboBox_SelectionChangeCommitted);
	// 
	// mLabel
	// 
	this->mLabel->AutoSize = true;
	this->mLabel->Location = System::Drawing::Point(8, 14);
	this->mLabel->Name = L"mLabel";
	this->mLabel->Size = System::Drawing::Size(79, 13);
	this->mLabel->TabIndex = 1;
	this->mLabel->Text = L"RenderSystem:";
	// 
	// mDataGridView
	// 
	this->mDataGridView->AllowUserToAddRows = false;
	this->mDataGridView->AllowUserToDeleteRows = false;
	this->mDataGridView->AutoSizeColumnsMode = System::Windows::Forms::DataGridViewAutoSizeColumnsMode::Fill;
	this->mDataGridView->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
	this->mDataGridView->ColumnHeadersVisible = false;
	this->mDataGridView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^  >(2) {this->mOptionName, 
		this->mOptionValue});
	this->mDataGridView->EditMode = System::Windows::Forms::DataGridViewEditMode::EditProgrammatically;
	this->mDataGridView->Location = System::Drawing::Point(8, 38);
	this->mDataGridView->Name = L"mDataGridView";
	this->mDataGridView->RowHeadersBorderStyle = System::Windows::Forms::DataGridViewHeaderBorderStyle::None;
	this->mDataGridView->RowHeadersVisible = false;
	this->mDataGridView->RowHeadersWidthSizeMode = System::Windows::Forms::DataGridViewRowHeadersWidthSizeMode::AutoSizeToAllHeaders;
	this->mDataGridView->ShowEditingIcon = false;
	this->mDataGridView->Size = System::Drawing::Size(600, 332);
	this->mDataGridView->TabIndex = 0;
	// 
	// mOptionName
	// 
	this->mOptionName->HeaderText = L"OptionName";
	this->mOptionName->Name = L"mOptionName";
	// 
	// mOptionValue
	// 
	this->mOptionValue->HeaderText = L"OptionValue";
	this->mOptionValue->Name = L"mOptionValue";
	this->mOptionValue->Resizable = System::Windows::Forms::DataGridViewTriState::True;
	this->mOptionValue->SortMode = System::Windows::Forms::DataGridViewColumnSortMode::Automatic;
	// 
	// mButtonSave
	// 
	this->mButtonSave->Location = System::Drawing::Point(496, 409);
	this->mButtonSave->Name = L"mButtonSave";
	this->mButtonSave->Size = System::Drawing::Size(55, 22);
	this->mButtonSave->TabIndex = 1;
	this->mButtonSave->Text = L"Save";
	this->mButtonSave->UseVisualStyleBackColor = true;
	this->mButtonSave->Click += gcnew System::EventHandler(this, &EditorOptionsForm::mButtonSave_Click);
	// 
	// mButtonCancel
	// 
	this->mButtonCancel->DialogResult = System::Windows::Forms::DialogResult::Cancel;
	this->mButtonCancel->Location = System::Drawing::Point(557, 409);
	this->mButtonCancel->Name = L"mButtonCancel";
	this->mButtonCancel->Size = System::Drawing::Size(55, 22);
	this->mButtonCancel->TabIndex = 2;
	this->mButtonCancel->Text = L"Cancel";
	this->mButtonCancel->UseVisualStyleBackColor = true;
	this->mButtonCancel->Click += gcnew System::EventHandler(this, &EditorOptionsForm::mButtonCancel_Click);
	// 
	// EditorOptionsForm
	// 
	this->AcceptButton = this->mButtonSave;
	this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
	this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
	this->CancelButton = this->mButtonCancel;
	this->ClientSize = System::Drawing::Size(624, 442);
	this->ControlBox = false;
	this->Controls->Add(this->mButtonCancel);
	this->Controls->Add(this->mButtonSave);
	this->Controls->Add(this->mTabControl);
	this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::SizableToolWindow;
	this->Name = L"EditorOptionsForm";
	this->Text = L"Editor Options";
	this->mTabControl->ResumeLayout(false);
	this->mTabPageRenderSystem->ResumeLayout(false);
	this->mTabPageRenderSystem->PerformLayout();
	(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->mDataGridView))->EndInit();
	this->ResumeLayout(false);

}

System::Void EditorOptionsForm::mButtonSave_Click(System::Object^  sender, System::EventArgs^  e)
{
	RenderSystem*rs=Ogre::Root::getSingletonPtr()->getRenderSystemByName(mRsList->at(mComboBox->SelectedIndex)->getName());		
	ConfigOptionMap opts=rs->getConfigOptions();
	ConfigOptionMap::iterator pOpt = opts.begin();
	int i=0;
	Ogre::String str;
	while (pOpt!=opts.end())
	{
		if(!(pOpt->second.name == "Full Screen" || pOpt->second.name=="Video Mode"))
		{
			char*charstr=(char*)(void*)Marshal::StringToHGlobalAnsi(mDataGridView->Rows[i]->Cells[1]->Value->ToString());
			str+=pOpt->first+":"+ Ogre::String(charstr)+"\n";
			Marshal::FreeHGlobal((IntPtr)(void*)charstr);
			rs->setConfigOption(pOpt->first,Ogre::String((char*)(void*)Marshal::StringToHGlobalAnsi(mDataGridView->Rows[i]->Cells[1]->Value->ToString())));
			i++;
		}
		++pOpt;
	}

	rs->validateConfigOptions();
	Root::getSingletonPtr()->setRenderSystem(rs);
	Root::getSingletonPtr()->saveConfig();
	Close();
}

System::Void EditorOptionsForm::mButtonCancel_Click(System::Object^  sender, System::EventArgs^  e)
{
	Close();
}

void EditorOptionsForm::updateDatagrid()
{
	mDataGridView->Rows->Clear();

	// Iterate through options
	ConfigOptionMap opts=mRsList->at(mComboBox->SelectedIndex)->getConfigOptions();
	ConfigOptionMap::iterator pOpt = opts.begin();

	while (pOpt!=opts.end())
	{
		bool missingValue=true;
		if(!(pOpt->second.name == "Full Screen" || pOpt->second.name=="Video Mode"))
		{
			int rowNb=mDataGridView->Rows->Add(gcnew System::String(pOpt->second.name.c_str()));	
			for(int i=0;i<(int)pOpt->second.possibleValues.size();i++)
			{
				int item= ((DataGridViewComboBoxCell^)mDataGridView->Rows[rowNb]->Cells[1])->Items->Add(gcnew System::String(pOpt->second.possibleValues.at(i).c_str()));
				if(pOpt->second.possibleValues.at(i)==pOpt->second.currentValue)
				{
					missingValue=false;
					mDataGridView->Rows[rowNb]->Cells[1]->Value=((DataGridViewComboBoxCell^)mDataGridView->Rows[rowNb]->Cells[1])->Items[item];									
				}
			}
			if(missingValue)
				mDataGridView->Rows[rowNb]->Cells[1]->Value=((DataGridViewComboBoxCell^)mDataGridView->Rows[rowNb]->Cells[1])->Items[0];
		}
		++pOpt;
	}
}

System::Void EditorOptionsForm::mComboBox_SelectionChangeCommitted(System::Object^  sender, System::EventArgs^  e)
{
	updateDatagrid();
}
